using System;
using System.Diagnostics.Metrics;
using System.IO;
using System.Net.NetworkInformation;

// 定义组件接口
public interface IComponent
{
    void SampleOperation();
}

// 定义具体组件类
public class ConcreteComponent : IComponent
{
    public void SampleOperation()
    {
        Console.WriteLine("ConcreteComponent: Executing operation.");
    }
}

// 定义装饰器抽象类
public abstract class Decorator : IComponent
{
    protected IComponent Component;

    public Decorator(IComponent component)
    {
        Component = component;
    }

    public virtual void SampleOperation()
    {
        Component.SampleOperation();
    }
}

// 定义具体装饰器类A
public class ConcreteDecoratorA : Decorator
{
    public ConcreteDecoratorA(IComponent component) : base(component)
    {
    }

    public override void SampleOperation()
    {
        Console.WriteLine("ConcreteDecoratorA: Adding additional behavior before.");
        base.SampleOperation();
        Console.WriteLine("ConcreteDecoratorA: Adding additional behavior after.");
    }
}

// 定义具体装饰器类B
public class ConcreteDecoratorB : Decorator
{
    public ConcreteDecoratorB(IComponent component) : base(component)
    {
    }

    public override void SampleOperation()
    {
        Console.WriteLine("ConcreteDecoratorB: Adding additional behavior before.");
        base.SampleOperation();
        Console.WriteLine("ConcreteDecoratorB: Adding additional behavior after.");
    }
}

// 客户端代码
public class Client
{
    public static void Main()
    {
        IComponent component = new ConcreteComponent();
        component.SampleOperation();

        Console.WriteLine("\nAfter applying ConcreteDecoratorA:");
        IComponent decoratorA = new ConcreteDecoratorA(component);
        decoratorA.SampleOperation();

        Console.WriteLine("\nAfter applying ConcreteDecoratorB:");
        IComponent decoratorB = new ConcreteDecoratorB(decoratorA);
        decoratorB.SampleOperation();
    }
}

//输出结果：
//ConcreteComponent: Executing operation.

//After applying ConcreteDecoratorA:
//ConcreteDecoratorA: Adding additional behavior before.
//ConcreteComponent: Executing operation.
//ConcreteDecoratorA: Adding additional behavior after.

//After applying ConcreteDecoratorB:
//ConcreteDecoratorB: Adding additional behavior before.
//ConcreteDecoratorA: Adding additional behavior before.
//ConcreteComponent: Executing operation.
//ConcreteDecoratorA: Adding additional behavior after.
//ConcreteDecoratorB: Adding additional behavior after.